package com.ui.home.adapter;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bean.SearchListBean;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.util.MultiTypeDelegate;
import com.ddd.wandemo.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class SearchResultRvAdapter extends BaseQuickAdapter<SearchListBean.DatasBean, BaseViewHolder> {
    private static final int PIC = 10000;
    private static final int TEXT = 10001;

    public SearchResultRvAdapter(@Nullable List<SearchListBean.DatasBean> data) {
        super(data);
        setMultiTypeDelegate(new MultiTypeDelegate<SearchListBean.DatasBean>() {
            @Override
            protected int getItemType(SearchListBean.DatasBean datasBean) {
                String envelopePic = datasBean.getEnvelopePic();
                return TextUtils.isEmpty(envelopePic) ? TEXT : PIC;
            }
        });
        getMultiTypeDelegate()
                .registerItemType(PIC, R.layout.item_search_result_pic)
                .registerItemType(TEXT, R.layout.item_home_article);
    }

    @Override
    protected void convert(BaseViewHolder helper, SearchListBean.DatasBean item) {
        helper.setText(R.id.tv_article_title,item.getTitle())
                .setText(R.id.tv_article_author,item.getAuthor())
                .setText(R.id.tv_article_time,new SimpleDateFormat("yyyy-MM-dd").format(new Date(item.getPublishTime())))
                .setText(R.id.tv_article_chapter,item.getChapterName());
        switch (helper.getItemViewType()) {
            case PIC:
                // do something
                Glide.with(mContext).load(item.getEnvelopePic()).into((ImageView) helper.getView(R.id.iv_search_pic));
                break;
        }
    }
}
